//
//  FoodListViewController.m
//  Funny
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 King. All rights reserved.
//

#import "FoodListViewController.h"
#import "FoodListTableViewCell.h"
#import "FoodListModel.h"

@interface FoodListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSString *URLStr;
@property (nonatomic, strong)NSMutableArray *listArr;
@property (nonatomic, assign)NSInteger page;
@end

@implementation FoodListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.page = 2;
    [self handle];
    [self.view addSubview:self.tableView];
}

- (void)handle
{
    if (self.num == 1) {
        self.URLStr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/poi_list?category_id=32,147,148&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&orderby=popular&page=1&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&types=&v=1",self.cityID];
    }else
    {
        self.URLStr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/poi_list?category_id=78&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&orderby=popular&page=1&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&types=&v=1",self.cityID];
    }
    
    [HandleData handleDataWithUrl:self.URLStr completion:^(id data) {
        [self.listArr removeAllObjects];
        NSDictionary *dic1 = data;
        NSDictionary *dic2 = [dic1 objectForKey:@"data"];
        NSArray *arr = [dic2 objectForKey:@"entry"];
        for (NSDictionary *dic in arr) {
            FoodListModel *model = [FoodListModel modelWithDic:dic];
            [self.listArr addObject:model];
            self.page = 2;
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    }];
}

- (NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [[NSMutableArray alloc]init];
    }
    return _listArr;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        CGRect rect = {{0,0},{ScreenWidth,ScreenHeight}};
        _tableView = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:self.tableView];
        _tableView.separatorStyle = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[FoodListTableViewCell class] forCellReuseIdentifier:@"foodlist"];
        //下拉刷新
        self.tableView.header = [MyRefreshHeaderGif headerWithRefreshingTarget:self refreshingAction:@selector(handle)];
        
        //上拉加载
        self.tableView.footer = [MyRefreshFooterGif footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _tableView;
}

//上拉加载更多
- (void)loadMoreData
{
    
    if (self.num == 1) {
        self.URLStr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/poi_list?category_id=32,147,148&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&orderby=popular&page=%ld&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&types=&v=1",self.cityID,(long)self.page];
    }else
    {
        self.URLStr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/poi_list?category_id=78&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&orderby=popular&page=%ld&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&types=&v=1",self.cityID,(long)self.page];
    }

    [HandleData handleDataWithUrl:self.URLStr completion:^(id data) {
        NSDictionary *dic1 = data;
        NSDictionary *dic2 = [dic1 objectForKey:@"data"];
        NSArray *arr = [dic2 objectForKey:@"entry"];
        for (NSDictionary *dic in arr) {
            FoodListModel *model = [FoodListModel modelWithDic:dic];
            [self.listArr addObject:model];
            self.page = self.page + 1;
        }
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodlist"];
    FoodListModel *model = [self.listArr objectAtIndex:indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning@2x.png"]];
    cell.chname.text = model.chinesename;
    cell.enname.text = model.englishname;
    cell.beento.text = model.beenstr;
    cell.keykey.text = model.recommendstr;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDetailViewController *view = [[FoodDetailViewController alloc]init];
    FoodListModel *model = [self.listArr objectAtIndex:indexPath.row];
    view.ID = model.ID;
    [self presentViewController:view animated:NO completion:^{
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110 * H;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
